package components.contorls.superClass
{
    import components.core.UIComponent;
    import components.utils.EventListenerUtil;
    
    import flash.display.MovieClip;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Rectangle;

	/**
	 *滑块 
	 * @author zn
	 * 
	 */	
	[Event(name="change",type="flash.events.Event")]
    public class BaseSlider extends UIComponent
    {
        private var _value:Number = 0;

		private var _maxValue:Number=10;
		
		private var _minValue:Number=0;
		
		/**
		 *采用几位小数 
		 */		
		private var _fractionDigits:int=0;
		
		private var _trackBar:Sprite;
		
		private var _trackBarShape:Shape;
		
        public function BaseSlider()
        {
            super();
			
			EventListenerUtil.addEventListener(thumbButton,MouseEvent.MOUSE_DOWN,thumbButton_mouseDownHandler);
			EventListenerUtil.addEventListener(thumbButton,MouseEvent.MOUSE_UP,thumbButton_mouseUpHandler);
        }
		
        /***************************************
         *
         * 显示对象声明
         *
         * **************************************/

        public function get thumbButton():BaseButton
        {
            return getChildByName("thumbSkin") as BaseButton;
        }
		
		public function get trackMC():MovieClip
		{
			return getChildByName("trackSkin") as MovieClip;
		}

        /***************************************
         *
         * 验证
         *
         * **************************************/

        protected override function commitProperties():void
        {
            super.commitProperties();
			
			if (!_initialized)
			{
				_trackBar=new Sprite();
				_trackBar.visible=false;
				
				_trackBarShape=new Shape();
				_trackBarShape.graphics.beginFill(0,1);
				_trackBarShape.graphics.drawRect(0,0,1,1);
				_trackBarShape.graphics.endFill();
				_trackBar.addChild(_trackBarShape);
				
				addChildAt(_trackBar,1);
				
				_trackBarShape.width=width;
				_trackBarShape.height=height;
			}
        }

        protected override function measureSizes():Boolean
        {
            if(super.measureSizes())
			{
				if(this is BaseVSlider)
				{
					trackMC.height=height;
					_trackBar.y=thumbButton.height/2;
					_trackBarShape.height=height-thumbButton.height;
				}
				else
				{
					trackMC.width=width;
					_trackBar.x=thumbButton.width/2;
					_trackBarShape.width=width-thumbButton.width;
				}
				
				return true;
			}
			return false;
        }


        protected override function updateDisplayList():void
        {
            super.updateDisplayList();
			
			var trackSize:Number=0;
			var thumbSize:Number=0;
			
			if(this is BaseVSlider)
			{
				trackSize=_trackBar.height;
				thumbSize=thumbButton.height;
			}
			else
			{
				trackSize=_trackBar.width;
				thumbSize=thumbButton.width;
			}
			
			var point:Number=0;
			if(maxValue!=0)
				point=(value/maxValue)*trackSize;
			
			point=Math.max(point,0);
			point=Math.min(trackSize,point);
				
			if(this is BaseVSlider)
				thumbButton.y=point+_trackBar.y;
			else
				thumbButton.x=point+_trackBar.x;
        }

        /***************************************
         *
         * 组件定义
         *
         * **************************************/


        [Inspectable(defaultValue="0")]
        public function get value():Number
        {
            return _value;
        }

        public function set value(v:Number):void
        {
			_value = Number(v.toFixed(fractionDigits));
			
			checkValue();
            invalidate();
        }

		private function checkValue():void
		{
			_value=Math.max(minValue,value);
			_value=Math.min(value,maxValue);
		}

		[Inspectable(defaultValue="10")]
		public function get maxValue():Number
		{
			return _maxValue;
		}
		
		public function set maxValue(value:Number):void
		{
			_maxValue = Math.max(0,value);
			checkValue();
			invalidate();
		}
		
		[Inspectable(defaultValue="0")]
		public function get minValue():Number
		{
			return _minValue;
		}
		
		public function set minValue(value:Number):void
		{
			_minValue = value;
			checkValue();
			invalidate();
		}
		
		[Inspectable(defaultValue="0")]
		public function get fractionDigits():int
		{
			return _fractionDigits;
		}
		
		public function set fractionDigits(value:int):void
		{
			_fractionDigits = value;
		}
		
		
        /***************************************
         *
         * 其他代码
         *
         * **************************************/

        public override function destroy():void
        {
			thumbButton.removeEventListener(MouseEvent.MOUSE_DOWN,thumbButton_mouseDownHandler);
			thumbButton.removeEventListener(MouseEvent.MOUSE_UP,thumbButton_mouseUpHandler);
			
			removeStageEventListener();
			thumbButton.stopDrag();
			
            super.destroy();
			
			_value=NaN;
			_minValue=NaN;
			_maxValue=NaN;
			
        }
		
		public override function validateNow():void
		{
			super.validateNow();
			thumbButton.validateNow();
		}
		
		protected function thumbButton_mouseDownHandler(event:MouseEvent):void
		{
			stage.addEventListener(MouseEvent.MOUSE_MOVE,stage_mouseMoveHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP,stage_mouseUpHandler);
			
//			if(this is BaseVSlider)
//				thumbButton.startDrag(false,new Rectangle(thumbButton.x,0,0,trackMC.height));
//			else
//				thumbButton.startDrag(false,new Rectangle(0,thumbButton.y,trackMC.width,0));
		}
		
		protected function thumbButton_mouseUpHandler(event:MouseEvent):void
		{
			removeStageEventListener();
			thumbButton.stopDrag();
			checkValue();
			invalidate();
		}

		protected function stage_mouseMoveHandler(event:MouseEvent):void
		{
			if(maxValue==0)
			{
				value=0;
			}
			else
			{
				if(this is BaseVSlider)
					value=_trackBar.mouseY/_trackBar.height*maxValue;
				else
					value=_trackBar.mouseX/_trackBar.width*maxValue;
			}
			
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		protected function stage_mouseUpHandler(event:MouseEvent):void
		{
			removeStageEventListener();
//			thumbButton.stopDrag();
			
			checkValue();
			invalidate();
		}
		
		protected function removeStageEventListener():void
		{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,stage_mouseMoveHandler);
			stage.removeEventListener(MouseEvent.MOUSE_UP,stage_mouseUpHandler);
		}
    }
}